package com.ludaxu.google.protocol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.IOUtils;
import com.ludaxu.google.domain.AppInfo;
import com.ludaxu.google.tools.FileUtils;
import com.ludaxu.google.tools.UiUtils;

public class HomeProtocol extends BaseProtocol<List<AppInfo>> {
	private List<String> pictures;

	@Override
	public String getKey() {
		return "home";
	}

	@Override
	public List<AppInfo> paserJson(String json) {
		List<AppInfo> appInfos = new ArrayList<AppInfo>();
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray jsonArray2 = jsonObject.getJSONArray("picture");
			for (int i = 0; i < jsonArray2.length(); i++) {
				String string = jsonArray2.getString(i);
				pictures.add(string);

			}

			JSONArray jsonArray = jsonObject.getJSONArray("list");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				long id = jsonObj.getLong("id");
				String name = jsonObj.getString("name");
				String packageName = jsonObj.getString("packageName");
				String iconUrl = jsonObj.getString("iconUrl");
				float stars = Float.parseFloat(jsonObj.getString("stars"));
				long size = jsonObj.getLong("size");
				String downloadUrl = jsonObj.getString("downloadUrl");
				String des = jsonObj.getString("des");
				AppInfo info = new AppInfo(id, name, packageName, iconUrl,
						stars, size, downloadUrl, des);
				appInfos.add(info);
			}
			return appInfos;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public List<String> getPictures() {
		return pictures;
	}
	

}
